package com.rockcent.service.impl;

import com.rockcent.common.common.BusinessException;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.MessageAutoreply;
import com.rockcent.mall.lib.domain.MessageReplyRule;
import com.rockcent.mall.lib.domain.MessageReplyRuleContent;
import com.rockcent.mall.lib.domain.MessageReplyRuleKeyword;
import com.rockcent.mall.lib.domain.enumclass.WechatMessageCode;
import com.rockcent.mall.lib.domain.enumclass.WechatMessageType;
import com.rockcent.mall.lib.dto.MessageAutoreplyDto;
import com.rockcent.mall.lib.dto.MessageReplyRuleContentDto;
import com.rockcent.mall.lib.dto.MessageReplyRuleDto;
import com.rockcent.mall.lib.dto.MessageReplyRuleKeywordDto;
import com.rockcent.mall.lib.repository.MallWechatMessageConfigRepository;
import com.rockcent.mall.lib.repository.MallWechatMessageReplyRuleContentRepository;
import com.rockcent.mall.lib.repository.MallWechatMessageReplyRuleKeywordRepository;
import com.rockcent.mall.lib.repository.MallWechatMessageReplyRuleRepository;
import com.rockcent.service.MallWechatMessageConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by marco on 2017/5/24.
 */
@Service
public class MallWechatMessageConfigServiceImpl implements MallWechatMessageConfigService {

    @Autowired
    private MallWechatMessageConfigRepository mallWechatMessageConfigRepository;
    @Autowired
    private MallWechatMessageReplyRuleRepository mallWechatMessageReplyRuleRepository;
    @Autowired
    private MallWechatMessageReplyRuleKeywordRepository mallWechatMessageReplyRuleKeywordRepository;
    @Autowired
    private MallWechatMessageReplyRuleContentRepository mallWechatMessageReplyRuleContentRepository;

    /**
     * 通过代码查找记录
     * @param code
     * @return
     */
    @Override
    public MessageAutoreply findByCodeAndMallId(String code, Long mallId) {
        return mallWechatMessageConfigRepository.findByCodeAndMallId(WechatMessageCode.valueOf(code),mallId);
    }

    /**
     * 保存或更新配置信息
     * @param dto
     */
    @Override
    public void saveOrUpdate(MessageAutoreplyDto dto) {
        if(dto==null) {
            return;
        }
        MessageAutoreply messageAutoreply = findByCodeAndMallId(dto.getCode(),dto.getMallId());
        if(messageAutoreply==null) {
            messageAutoreply = new MessageAutoreply();
        }
        messageAutoreply.setCode(WechatMessageCode.valueOf(dto.getCode()));
        messageAutoreply.setType(WechatMessageType.valueOf(dto.getType()));
        messageAutoreply.setContent(dto.getContent());
        messageAutoreply.setMallId(dto.getMallId());
        messageAutoreply.setMediaId(dto.getMediaId());
        messageAutoreply.setMediaTitle(dto.getMediaTitle());
        messageAutoreply.setMediaDesc(dto.getMediaDesc());
        messageAutoreply.setMediaUrl(dto.getMediaUrl());
        mallWechatMessageConfigRepository.save(messageAutoreply);
    }

    /**
     * 删除配置信息
     * @param code
     * @param mallId
     */
    @Override
    public void delete(String code, Long mallId) {
        if(StringUtils.isEmpty(code) || mallId==null) {
            return;
        }
        MessageAutoreply messageAutoreply = findByCodeAndMallId(code, mallId);
        mallWechatMessageConfigRepository.delete(messageAutoreply);
    }

    /**
     * 删除规则下的关键词列表与内容列表
     * @param ruleId
     */
    @Override
    public void deleteKeywordAndContentByRuleId(Long ruleId) {
        if(ruleId==null) {
            return;
        }
        //删除关键词列表与内容列表
        List<MessageReplyRuleKeyword> keywordList = mallWechatMessageReplyRuleKeywordRepository.findByMessageReplyRuleId(ruleId);
        if (keywordList != null && keywordList.size() > 0) {
            mallWechatMessageReplyRuleKeywordRepository.delete(keywordList);
        }
        List<MessageReplyRuleContent> contentList = mallWechatMessageReplyRuleContentRepository.findByMessageReplyRuleId(ruleId);
        if (contentList != null && contentList.size() > 0) {
            mallWechatMessageReplyRuleContentRepository.delete(contentList);
        }
    }

    /**
     * 保存规则
     * @param dto
     */
    @Override
    public MessageReplyRule saveOrUpdate(MessageReplyRuleDto dto) {
        if(dto==null) {
            return null;
        }
        MessageReplyRule messageReplyRule = null;
        if(dto.getId()!=null && dto.getId().longValue()>0) {
            messageReplyRule = findRuleByIdAndMallId(dto.getId(),dto.getMallId());
            if(messageReplyRule==null) {
                throw new BusinessException("规则id不存在");
            }
        }
        if(messageReplyRule==null) {
            messageReplyRule = new MessageReplyRule();
        } else {
            deleteKeywordAndContentByRuleId(messageReplyRule.getId());
        }
        messageReplyRule.setName(dto.getName());
        messageReplyRule.setMallId(dto.getMallId());
        if(StringUtils.isNotEmpty(dto.getAllReply())) {
            messageReplyRule.setAllReply(Whether.valueOf(dto.getAllReply()));
        }
        MessageReplyRule rule = mallWechatMessageReplyRuleRepository.save(messageReplyRule);
        //关键词列表
        List<MessageReplyRuleKeywordDto> keywordDtos = dto.getKeywords();
        if(keywordDtos!=null && keywordDtos.size()>0) {
            List<MessageReplyRuleKeyword> keywordList = new ArrayList<>();
            for(MessageReplyRuleKeywordDto keywordDto:keywordDtos) {
                MessageReplyRuleKeyword keyword = new MessageReplyRuleKeyword();
                keyword.setMessageReplyRuleId(rule.getId());
                keyword.setKeyword(keywordDto.getKeyword());
                if(StringUtils.isNotEmpty(keywordDto.getAllMatch())) {
                    keyword.setAllMatch(Whether.valueOf(keywordDto.getAllMatch()));
                }
                keywordList.add(keyword);
            }
            mallWechatMessageReplyRuleKeywordRepository.save(keywordList);
        }
        //内容列表
        List<MessageReplyRuleContentDto> contents = dto.getContents();
        if(contents!=null && contents.size()>0) {
            List<MessageReplyRuleContent> contentList = new ArrayList<>();
            for(MessageReplyRuleContentDto contentDto:contents) {
                MessageReplyRuleContent content = new MessageReplyRuleContent();
                content.setMessageReplyRuleId(rule.getId());
                content.setType(WechatMessageType.valueOf(contentDto.getType()));
                content.setContent(contentDto.getContent());
                content.setMediaId(contentDto.getMediaId());
                content.setMediaTitle(contentDto.getMediaTitle());
                content.setMediaDesc(contentDto.getMediaDesc());
                content.setMediaPicUrl(contentDto.getMediaPicUrl());
                content.setMediaUrl(contentDto.getMediaUrl());
                String mediaCreateDate = contentDto.getMediaCreateDate();
                content.setMediaCreateDate(StringUtils.isEmpty(mediaCreateDate)?"":mediaCreateDate);
                contentList.add(content);
            }
            mallWechatMessageReplyRuleContentRepository.save(contentList);
        }
        return rule;
    }

    /**
     * 查询规则
     * @param id
     * @param mallId
     * @return
     */
    @Override
    public MessageReplyRule findRuleByIdAndMallId(Long id, Long mallId) {
        return mallWechatMessageReplyRuleRepository.findByIdAndMallId(id,mallId);
    }

    /**
     * 查询规则详情(包括关键词与内容列表)
     * @param id
     * @param mallId
     * @return
     */
    @Override
    public MessageReplyRuleDto findRuleKeywordContentByIdAndMallId(Long id, Long mallId) {
        MessageReplyRuleDto messageReplyRuleDto = null;
        //查询规则
        MessageReplyRule messageReplyRule = findRuleByIdAndMallId(id,mallId);
        if(messageReplyRule!=null) {
            messageReplyRuleDto = new MessageReplyRuleDto();
            messageReplyRuleDto.setId(messageReplyRule.getId());
            messageReplyRuleDto.setAllReply(String.valueOf(messageReplyRule.getAllReply()));
            messageReplyRuleDto.setMallId(messageReplyRule.getMallId());
            messageReplyRuleDto.setName(messageReplyRule.getName());
            //查询关键词列表
            List<MessageReplyRuleKeywordDto> keywordDtoList = new ArrayList<>();
            messageReplyRuleDto.setKeywords(keywordDtoList);
            List<MessageReplyRuleKeyword> keywordList = mallWechatMessageReplyRuleKeywordRepository.findByMessageReplyRuleId(messageReplyRule.getId());
            if (keywordList != null && keywordList.size() > 0) {
                for(MessageReplyRuleKeyword keyword:keywordList) {
                    MessageReplyRuleKeywordDto keywordDto = new MessageReplyRuleKeywordDto();
                    keywordDto.setKeyword(keyword.getKeyword());
                    keywordDto.setAllMatch(String.valueOf(keyword.getAllMatch()));
                    keywordDto.setMessageReplyRuleId(keyword.getMessageReplyRuleId());
                    keywordDtoList.add(keywordDto);
                }
            }
            //查询内容列表
            List<MessageReplyRuleContentDto> contentDtoList = new ArrayList<>();
            messageReplyRuleDto.setContents(contentDtoList);
            List<MessageReplyRuleContent> contentList = mallWechatMessageReplyRuleContentRepository.findByMessageReplyRuleId(messageReplyRule.getId());
            if (contentList != null && contentList.size() > 0) {
                for(MessageReplyRuleContent ruleContent:contentList) {
                    MessageReplyRuleContentDto content = new MessageReplyRuleContentDto();
                    content.setMessageReplyRuleId(ruleContent.getMessageReplyRuleId());
                    content.setType(String.valueOf(ruleContent.getType()));
                    content.setContent(ruleContent.getContent());
                    content.setMediaId(ruleContent.getMediaId());
                    content.setMediaTitle(ruleContent.getMediaTitle());
                    content.setMediaDesc(ruleContent.getMediaDesc());
                    content.setMediaPicUrl(ruleContent.getMediaPicUrl());
                    content.setMediaUrl(ruleContent.getMediaUrl());
                    content.setMediaCreateDate(ruleContent.getMediaCreateDate());
                    contentDtoList.add(content);
                }
            }
        }
        return messageReplyRuleDto;
    }

    /**
     * 删除规则配置信息
     * @param id
     * @param mallId
     */
    @Override
    public void delete(Long id, Long mallId) {
        if(id==null || mallId==null) {
            return;
        }
        MessageReplyRule messageReplyRule = findRuleByIdAndMallId(id,mallId);
        if(messageReplyRule!=null) {
            deleteKeywordAndContentByRuleId(messageReplyRule.getId());
        }
        mallWechatMessageReplyRuleRepository.delete(messageReplyRule);
    }

    /**
     * 通过平台ID查找规则列表
     * @param mallId
     * @return
     */
    @Override
    public List<MessageReplyRuleDto> findRulesByMallId(Long mallId) {
        if(mallId==null) {
           return null;
        }
        List<MessageReplyRuleDto> messageReplyRuleDtos = null;
        List<MessageReplyRule> messageReplyRules = mallWechatMessageReplyRuleRepository.findByMallId(mallId);
        if(messageReplyRules!=null && messageReplyRules.size()>0) {
            messageReplyRuleDtos = new ArrayList<>();
            for(MessageReplyRule messageReplyRule:messageReplyRules) {
                messageReplyRuleDtos.add(findRuleKeywordContentByIdAndMallId(messageReplyRule.getId(),mallId));
            }
        }
        return messageReplyRuleDtos;
    }

    /**
     * 通过关键字查找规则列表
     * @param keyword
     * @param mallId
     * @return
     */
    @Override
    public List<MessageReplyRule> findRulesByKeywordAndMallId(String keyword, Long mallId) {
        return mallWechatMessageReplyRuleRepository.findRulesByKeywordAndMallId(keyword,mallId);
    }

    /**
     * 通过规则ID随机找一条内容信息
     * @param ruleId
     * @return
     */
    @Override
    public MessageReplyRuleContent findRuleContentByRuleId(Long ruleId) {
        return mallWechatMessageReplyRuleContentRepository.findOneMessageReplyContentByruleId(ruleId);
    }

    /**
     * 通过规则ID内容信息列表
     * @param ruleId
     * @return
     */
    @Override
    public List<MessageReplyRuleContent> findRuleContentsByRuleId(Long ruleId) {
        return mallWechatMessageReplyRuleContentRepository.findByMessageReplyRuleId(ruleId);
    }
}
